<template>
  <div class="home-box">
    <el-form :inline="true" :model="searchForm">
      <el-form-item label="员工工号">
        <el-input v-model="searchForm.empId" placeholder="员工工号" />
      </el-form-item>
      <el-form-item label="员工姓名">
        <el-input
          v-model="searchForm.empName"
          placeholder="员工姓名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="searchEvent" type="primary">查询 </el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="update" type="primary">重置 </el-button>
      </el-form-item>
      <el-form-item>
        <el-button color="#ff983f" :dark="isDark" plain icon="Download" @click="handleOut"
    >导出</el-button>
      </el-form-item>
      <el-form-item >
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button circle icon="Refresh" @click="update" />
        </el-tooltip>
      </el-form-item>
    </el-form>
  </div>
    <el-table
      :data="ScoreRuleList"
      border
      style="width: 95%; margin: 20px auto;border-radius: 8px; margin: 20px auto;box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);"
      :header-cell-style="{
          'background': '#337ecc !important',
          'color': '#ffffff',
          'border': 'none !important'
        }">
      <el-table-column prop="empName" label="被考核人" width="" />
	  <el-table-column prop="position" label="岗位名称" width="" />
	  <el-table-column prop="deptName" label="部门" width="" />
	  <el-table-column prop="type" label="类型" width="170"/>
      <el-table-column prop="target" label="考核条目" width="170"/>
      <el-table-column prop="scorePercent" label="考核条目占比%" />
      <el-table-column prop="assessorName" label="评分人" width="80"/>
      <el-table-column prop="assessorPercent" label="评分人占比%" width="110"/>
	  <el-table-column prop="score" label="评分" width="80"/>
	  <el-table-column prop="grade" label="得分" width="110"/>
	  <el-table-column prop="remark" label="评分说明" width="120"/>
	  <el-table-column prop="originalScore" label="原分值/￥" width="90" v-auth="'编辑评分'"/>
      <el-table-column prop="correctedValue" label="修正值/￥" width="90" v-auth="'编辑评分'"/>
      <el-table-column
      prop="state"
      label="当前状态"
      width="95"
    >
      <template #default="scope">
        <el-tag
          :type="scope.row.tag === 'Home' ? '' : 'success'"
          disable-transitions
          >{{ scope.row.state === 0 ? "已评分" : "已归档" }}</el-tag
        >
      </template>
    </el-table-column>
      <el-table-column label="操作" width="150" v-auth="'编辑评分'">
      <template #default="scope">
		  <!-- 未完成 先隐藏 -->
        <el-button size="small" @click="handleEdit(scope.$index, scope.row)" 
          >编辑</el-button
        >
		
		<!-- 危险 先隐藏 -->
        <!-- <el-button
          size="small"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button> -->
        
      </template>
    </el-table-column>
    </el-table>
    <pagination
    class="pagination"
    v-show="ScoreListPage.total > 0"
    :total="ScoreListPage.total"
    v-model:page="searchForm.page"
    v-model:limit="ScoreListPage.size"
    @pagination="handlePagination"
  />
  <Dialog
    v-model="DialogVisible"
    :close="close"
    :update="update"
    :select="select"
  ></Dialog>
</template>

<script setup>
import { ref, onBeforeMount, reactive, getCurrentInstance,watch } from "vue";
import {getEmpScoreNowPage,reDelete,getEmpScoreNowSearch} from "@/api/empScore"
import Dialog from "./scoreDialog";
import { ElMessage } from "element-plus";
import {downLoadEmpScore} from "@/api/downExcel"

const searchForm = ref({
  empId: "",
  empName: "",
  page: 1,
  page_size: 10,
});
const ScoreListPage = ref([]);
const ScoreRuleList = ref([]);
onBeforeMount(() => {
  getEmpScoreNowPage(searchForm.value).then((data) => {
	
	
	
	let item = data.data.records
	
	for(var i = 0;i <item.length;i++){
		// console.log(item[i].correctedValue)
		if(item[i].correctedValue == null ){
			item[i].grade = (Number(item[i].score)* Number(item[i].scorePercent)/100 * Number(item[i].assessorPercent)/100).toFixed(2) ;
		}else{
			item[i].grade = (Number(item[i].correctedValue)* Number(item[i].scorePercent)/100 * Number(item[i].assessorPercent)/100).toFixed(2) ;
		}
		
		
		item[i].scorePercent += "%"
		item[i].assessorPercent += "%"
	}
	
    ScoreListPage.value = data.data;
    ScoreRuleList.value = data.data.records;
  });
});
watch(
  ScoreRuleList,
  (newVal) => {
    
  },
  { deep: true }
);

const handleOut=()=>{
  downLoadEmpScore()
}

const searchEvent=()=>{
  getEmpScoreNowSearch(searchForm.value).then((data) => {
	  let item = data.data.records
	  
	  for(var i = 0;i <item.length;i++){
	  	if(item[i].correctedValue == null ){
	  		item[i].grade = (Number(item[i].score)* Number(item[i].scorePercent)/100 * Number(item[i].assessorPercent)/100).toFixed(2) ;
	  	}else{
	  		item[i].grade = (Number(item[i].correctedValue)* Number(item[i].scorePercent)/100 * Number(item[i].assessorPercent)/100).toFixed(2) ;
	  	}
	  	item[i].scorePercent += "%"
	  	item[i].assessorPercent += "%"
	  }
    ScoreListPage.value = data.data;
    ScoreRuleList.value = data.data.records;
  });
}

const handlePagination=(value)=>{
  if(searchForm.value.empId==""&&searchForm.value.empName==""){
    getEmpScoreNowPage(value).then(data=>{
		let item = data.data.records
		
		for(var i = 0;i <item.length;i++){
			if(item[i].correctedValue == null ){
				item[i].grade = (Number(item[i].score)* Number(item[i].scorePercent)/100 * Number(item[i].assessorPercent)/100).toFixed(2) ;
			}else{
				item[i].grade = (Number(item[i].correctedValue)* Number(item[i].scorePercent)/100 * Number(item[i].assessorPercent)/100).toFixed(2) ;
			}
			item[i].scorePercent += "%"
			item[i].assessorPercent += "%"
		}
        ScoreListPage.value = data.data;
        ScoreRuleList.value = data.data.records;
    })
  }
  else{
    searchForm.value.page=value.page;
    searchForm.value.page_size=value.page_size
    getEmpScoreNowSearch(searchForm.value).then(data=>{
		let item = data.data.records
		
		for(var i = 0;i <item.length;i++){
			if(item[i].correctedValue == null ){
				item[i].grade = (Number(item[i].score)* Number(item[i].scorePercent)/100 * Number(item[i].assessorPercent)/100).toFixed(2) ;
			}else{
				item[i].grade = (Number(item[i].correctedValue)* Number(item[i].scorePercent)/100 * Number(item[i].assessorPercent)/100).toFixed(2) ;
			}
			item[i].scorePercent += "%"
			item[i].assessorPercent += "%"
		}
      ScoreListPage.value = data.data;
      ScoreRuleList.value = data.data.records;
    })
  } 
}

let select=reactive()
const DialogVisible = ref(false);
const handleEdit=(index,row)=>{
  DialogVisible.value = true;
  select=row
}
const handleDelete=(index,row)=>{
  reDelete({empId:row.empId,id:row.empScoreId}).then(data=>{
    if (data.code == 200) {
      ElMessage({
        message: "修正成功",
        type: "success",
      });
	  //防止搜索人时改变逻辑
      update()
    } else {
      ElMessage.error("修正失败");
    }
  })
}

const update = () => {
	//防止搜索人时改变逻辑
  searchForm.value.empId=""
  searchForm.value.empName=""
  getEmpScoreNowPage(searchForm.value).then((data) => {
    if (data.code == 200) {
      ElMessage({
        message: "更新成功",
        type: "success",
      });
	  let item = data.data.records
	  
	  for(var i = 0;i <item.length;i++){
	  	if(item[i].correctedValue == null ){
	  		item[i].grade = (Number(item[i].score)* Number(item[i].scorePercent)/100 * Number(item[i].assessorPercent)/100).toFixed(2) ;
	  	}else{
	  		item[i].grade = (Number(item[i].correctedValue)* Number(item[i].scorePercent)/100 * Number(item[i].assessorPercent)/100).toFixed(2) ;
	  	}
		item[i].scorePercent += "%"
		item[i].assessorPercent += "%"
	  }
      ScoreListPage.value = data.data;
      ScoreRuleList.value = data.data.records;
    } else {
      ElMessage.error("更新失败");
    }
  });
};

const close=()=>{
  DialogVisible.value = false;
}
</script>
<style lang="scss" scoped>
.el-card {
  border-radius: 10px;
}
</style>
